const { insuresModel } = require('./models/insuresModel');

//根据ID保险产品
module.exports.getInsureById = async params => {
    return await insuresModel.find(params).populate('typesId');
}

//获取所有保险产品
module.exports.getInsures = async ({ type = 'name', value = '', pageSize = 3, currentPage = 1 }) => {
    // 获取数据总条数
    const total = await insuresModel.countDocuments({
        isDel: false, [type]: { $regex: value, $options: '$i' }
    });

    // 模糊查询
    const insures = await insuresModel
        .find({ isDel: false, [type]: { $regex: value, $options: '$i' } })
        .populate('typesId')
        .limit(pageSize - 0)
        .skip((currentPage - 1) * pageSize);
    return { total, insures };
}

//新增保险产品
module.exports.addInsure = async params => {
    params.brief == '' ? params.brief = '27项出境保障多语言国际救援' : 1;
    params.coverMap == '' ? params.coverMap = 'index_ly04.jpg' : 1;
    params.coverDetails == '' ? params.coverDetails = 'particulars.jpg' : 1;
    return await insuresModel.create(params);
}

//修改保险产品
module.exports.updateInsureById = async params => {
    return await insuresModel.updateOne({ _id: params._id }, params);
}

//删除保险产品
module.exports.delInsureById = async params => {
    return await insuresModel.updateOne(params, { isDel: true });
}